﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Collections.Generic;
using OOutlook = Microsoft.Office.Interop.Outlook;

namespace Ksamuelsen.Office.Outlook {
    /// <summary>
    /// Contains a reference to an Outlook folder
    /// </summary>
    /// <remarks>
    /// This class can be used to hold a reference to an Outlook folder. Use this class when you don't want to keep the folder object in memory, or need to store the reference.
    /// </remarks>
    public class OutlookFolderRef {
        private string _storeId;
        private string _folderId;
        private string _storeName;
        private string _folderName;
        private int    _hashCode = 0;

        public OutlookFolderRef(string storeid, string storeName, string folderid, string folderName) {
            _storeId    = storeid;
            _folderId   = folderid;
            _storeName  = storeName;
            _folderName = folderName;

        }

        public OutlookFolderRef(string storeid, string folderid) : this(storeid, String.Empty, folderid, String.Empty) {
        }

        public OutlookFolderRef(string id) : this(id.Substring(0, id.IndexOf("@")), id.Substring(id.IndexOf("@") + 1)) {
        }

        /// <summary>
        /// Gets a single id represented as a string. The id is a concatenation of the store id and folder id, delimited by [@].
        /// </summary>
        public string Id { get { return _storeId + "@" + _folderId; } }
        public string StoreId { get { return _storeId; } }
        public string FolderId { get { return _folderId; } }

        public override bool Equals(object obj) {
            if (obj == null) {
                return false;
            }

            OutlookFolderRef p = obj as OutlookFolderRef;
            if ((System.Object)p == null) {
                return false;
            }

            return (_storeId.Equals(p._storeId) && (_folderId.Equals(p._folderId)));
        }

        /// <summary>
        /// Gets the Outlook folder object that this class references
        /// </summary>
        public OOutlook.Folder Folder {
            get {
                return OutlookManager.Application.Session.GetFolderFromID(_folderId, _storeId) as OOutlook.Folder;
            }
        }

        public override int GetHashCode() {
            if (_hashCode == 0)
                _hashCode = (_storeId + "@" + _folderId).GetHashCode();
            return _hashCode;
        }

        public override string ToString() {
            return _storeName + " - " + _folderName;
        }

    }
}
